package org.prs.persistence;

import com.prs.util.PrsException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.Medical;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class MedicalDatabasePersistence {

    private Connection conn;
    private ResultSet result;

    public boolean insertHealthAssessment(Medical healthAssessment) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement insertHealthAssessment = null;
        try {
            insertHealthAssessment = conn.prepareStatement("insert into health set "
                    + " dateofassessment=STR_TO_DATE(?,'%d/%m/%Y'), details=?, childid=?, ailment=?, "
                    + "doctorcomment=?, hospitalvisited=?, cost=?");

            insertHealthAssessment.setString(1, healthAssessment.getDateOfAssessment());
            insertHealthAssessment.setString(2, healthAssessment.getDetails());
            insertHealthAssessment.setInt(3, healthAssessment.getChildId());
            insertHealthAssessment.setString(4, healthAssessment.getAilment());
            insertHealthAssessment.setString(5, healthAssessment.getDoctorComment());
            insertHealthAssessment.setString(6, healthAssessment.getHospitalVisited());
            insertHealthAssessment.setDouble(7, healthAssessment.getCost());
            insertHealthAssessment.executeUpdate();
        } catch (SQLException e) {
            inserted = false;
            throw new PrsException(e);
        }
        return inserted;
    }

    public Medical getHealth(int healthId) {
        Medical healthAssessment = new Medical();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectHealthById = null;
        try {
            selectHealthById = conn.prepareStatement("select * from health where healthid=?");
            selectHealthById.setInt(1, healthId);
            while (result.next()) {
                healthAssessment = getHealthAssessment(result);
            }
        } catch (SQLException e) {

            throw new PrsException(e);
        }
        return healthAssessment;
    }

    public List<Medical> getChildHealthAssessment(int childId) {
        List<Medical> healthAssessments = new ArrayList<Medical>();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectHealthAssessmentByChildId = null;
        try {
            selectHealthAssessmentByChildId = conn.prepareStatement("select * from health where childid=?");
            selectHealthAssessmentByChildId.setInt(1, childId);
            result=selectHealthAssessmentByChildId.executeQuery();
            while (result.next()) {
                healthAssessments.add(getHealthAssessment(result));
            }
        } catch (SQLException e) {
            throw new PrsException(e);
        }
        return healthAssessments;
    }

    private Medical getHealthAssessment(ResultSet result) {
        Medical healthAssessment = new Medical();
        try {
            healthAssessment.setChildId(result.getInt("childid"));
           healthAssessment.setDateOfAssessment(result.getString("dateofassessment"));
            healthAssessment.setDetails(result.getString("details"));
            healthAssessment.setHealthId(result.getInt("healthid"));
            healthAssessment.setHospitalVisited(result.getString("hospitalvisited"));
            healthAssessment.setDoctorComment(result.getString("doctorcomment"));
            healthAssessment.setAilment(result.getString("ailment"));
        } catch (SQLException e) {

            throw new PrsException(e);
        }
        return healthAssessment;
    }
}
